#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

int n;	

void count(int *a)
{
	
	int i,k=0,c=0;	
	printf("\nenter element you want to count: ");
	scanf("%d", &k);
	for (i=0; i<10; i++)
	{
		if (*(a+i)==k)
			c++;
	}
 	printf("\nNo. of occurences = %d",c);
}
void extend(int *a)
{
	int i,m,k;
	printf("\nEnter size of new array: ");
	scanf("%d", &m);
	a= (int*)realloc(a, (sizeof(int) * (n+m)));		
	printf("\nEnter m array elements");
	for(i=0; i<m; i++)
	{
		printf("\nEnter new array element %d : ", i);
		scanf("%d", &k);
		a[n+i]=k;
	}
	n+=m;
}
void append(int *a)
{
	
	int i,k;
	printf("\nenter element you want to append: ");
	scanf("%d", &k);
	a= (int*)realloc(a, (sizeof(int) * (n+1)));
	a[n]=k;
	n++;
}
void sort(int *a)
{
	
	int i,j,temp;
	for (j=0;j<n;j++)
	{
		for (i=0;i<n-j-1;i++)
		{
			if(a[i]>a[i+1])
			{
				temp=a[i+1];
				a[i+1]=a[i];
				a[i]=temp;
			}
		}
	}
}
void insert(int *a)
{
	int i,pos,e;
	printf("Position starts from 1. Enter position you want to enter: ");
	scanf("%d", &pos);
	if (pos<=0 || pos>n+1)
	{
		printf("invalid position");
	}
	else
	{
		a= (int*)realloc(a, (sizeof(int) * (n+1)));
		printf("\nenter element you want to enter: ");
		scanf("%d",&e);
		for(i=n-1;i>=pos-1;i--)
		{
			a[i+1]=a[i];
		}
		a[pos-1]=e;
		n++;
		display(a);
	}
}
void remove1(int *a)
{
	int i,e,pos,flag=0;
	printf("\nenter element you want to delete: ");
	scanf("%d",&e);
	for(i=0;i<n;i++)
	{
		if (a[i]==e)
		{
			pos=i;
			flag=1;
			break;
		}
	}
	if(flag==1)
	{
		for(i=pos;i<n;i++)
		{
			a[i]=a[i+1];
		}
		a= (int*)realloc(a, (sizeof(int) * (n-1)));
		n--;
	}
	else
	{
		printf("Element not present");
	}
}
void index1(int *a)
{
	int i,e,pos;
	printf("\nenter element you want to search: ");
	scanf("%d",&e);
	for(i=0;i<n;i++)
	{
		if(a[i]==e)
		{
			pos=i;
			break;
		}
	}
	printf("first occurence found at index:%d ", pos);
}
void reverse1(int *a)
{
	int i,temp;
	for(i=0;i<(n/2);i++)
	{
		temp=a[n-1-i];
		a[n-1-i]=a[i];
		a[i]=temp;
	}	
}
void pop(int *a)
{
	int ch,i;
	printf("\nenter index from you want to pop or enter -1 to pop out the last element: ");
	scanf("%d", &ch);
	if (ch>(n-1))
	{
		printf("\ninvalid index");
	}
	else if(ch==-1)
	{
		printf("\nelement poped out=%d", a[n-1]);		
		a= (int*)realloc(a, (sizeof(int) * (n-1)));
		n--;
	}
	else
	{
		printf("\nelement poped out=%d", a[ch]);
		for(i=ch;i<n;i++)
		{
			a[i]=a[i+1];
		}
		a= (int*)realloc(a, (sizeof(int) * (n-1)));
		n--;
	}
}	
void display(int *a)
{
	int i;	
	printf("\nModified array: ");
	for(i=0;i<n;i++)
	{
		printf("%d " ,a[i]);
	}
}
void main()
{
	int ch;
	int i;
	printf("\nEnter size of array: ");
	scanf("%d", &n);
	int *a = (int*)malloc(sizeof(int) * n);		
	printf("\nEnter n array elements");
	for(i=0; i<n; i++)
	{
		printf("\nEnter array element %d : ", i);
		scanf("%d", &a[i]); 
	}
	while(ch!=0)
	{
		printf("\nPress 1 for count");
		printf("\nPress 2 for append");	
		printf("\nPress 3 for sort");	
		printf("\nPress 4 for insert");	
		printf("\nPress 5 for remove");	
		printf("\nPress 6 for index");	
		printf("\nPress 7 for reverse");
		printf("\nPress 8 for extend");	
		printf("\nPress 9 for pop");
		printf("\nPress 10 to display current array");
		printf("\nPress 0 for exit");
		
		printf("\nEnter your choice: ");
		scanf("%d" ,&ch);
	
		switch (ch)
		{
			case 1:
			{
				count(a);
				break;
			}
			case 2:
			{
				append(a);
				display(a);
				break;
			}
			case 3:
			{
				sort(a);
				display(a);
				break;
			}
			case 4:
			{
				insert(a);
				break;
			}
			case 5:
			{
				remove1(a);
				display(a);
				break;
			}
			case 6:
			{
				index1(a);
				break;
			}
			case 7:
			{
				reverse1(a);
				display(a);
				break;
			}
			case 8:
			{
				extend(a);
				display(a);
				break;
			}
			case 9:
			{
				pop(a);
				display(a);
				break;
			}
			case 10:
			{
				display(a);
				break;
			}
			case 0:
			{
				break;
			}
				
			default:
			{
				printf("option not available");
			
			}
		}
	}	
}
	
